package edu.nf.project.webs.storeOperation;

import edu.nf.project.entitys.dto.storeOperation.WarehouseDTO;
import edu.nf.project.services.exception.derivedAnomalies.CustomerOrderException;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import edu.nf.project.services.storeOperation.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/warehouse")
public class WarehouseController extends BaseController {
    @Autowired
    private WarehouseService warehouseService;

    @GetMapping("/all")
    public ResultVO<List<WarehouseDTO>> getAll() {
        try {
            List<WarehouseDTO> cargoOrders = warehouseService.getAll();
            return success(cargoOrders);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("/plusOne")
    public ResultVO<Boolean> inventoryPlusOne(Integer cargoId, Integer quantity) {
        try {
            Boolean flag = warehouseService.inventoryPlus(cargoId, quantity);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("/minusOne")
    public ResultVO<Boolean> inventoryMinusOne(Integer cargoId, Integer quantity) {
        try {
            Boolean flag = warehouseService.inventoryMinus(cargoId, quantity);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }
}
